<template>
  <div>
    <el-form size="mini" inline>
      <el-form-item label="年份">
        <el-date-picker
          v-model="yearsumm"
          type="year"
          placeholder="请选择"
          value-format="yyyy"
          clearable
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <select-company @setCompanyName="gelectCompany"></select-company>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="querySumm">查询</el-button>
      </el-form-item>
    </el-form>
    <Ca-rule-table
      :DataList="summaryList"
      :header="header"
      v-if="isload"
      @cellCilck="cellCilck"
      cellField="经营成本"
      :iscellCilck="true"
    >
    </Ca-rule-table>
    <paging
      :currentpage="currentpage"
      :currentlimit="currentlimit"
      :total="total"
      @setpage="getpage"
      @setlimit="getlimit"
    >
    </paging>
    <el-dialog
      :visible.sync="iscellCilck"
      top="23vh"
      width="20%"
      height="40%"
      v-dialogDrag
    >
      <el-form size="mini" label-width="80px">
        <el-form-item label="经营成本">
          <el-input clearable v-model="addform.operatingCost"> </el-input>
        </el-form-item>
        <el-form-item label="年份">
          <el-date-picker
            v-model="addform.year"
            type="year"
            style="width:100%;"
            placeholder="请选择"
            value-format="yyyy"
            clearable
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注">
          <el-input clearable v-model="addform.remark"></el-input>
        </el-form-item>
        <el-form-item label=" ">
          <el-button type="primary" @click="save">保存</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import selectCompany from "@/components/Ca-select/select-company";
import CaRuleTable from "@/components/Ca-table/Ca-rule-table";
import paging from "@/components/paging/paging";
import {
  apigetProjectDepSummary,
  apisaveProjectDepSummary,
  apigetProjectSummaryByDep,
  apisaveProjectSummary
} from "@/request/api.js";
export default {
  data() {
    return {
      addform: {},
      iscellCilck: false,
      yearsumm: "",
      currentpage: 1,
      currentlimit: 15,
      summaryList: [],
      total: 1,
      header: [],
      build_header: [
        ["合同编号", "manage_contract_num"],
        ["项目名称", "manage_contract_name"],
        ["建设单位", "manage_contract_firstParty"],
        [
          "经营汇总表",
          "",
          "",
          "",
          [
            [
              "合同情况",
              "",
              "",
              "",
              [
                ["合同金额", "manage_contract_amount"],
                ["最终结算金额", "settlementAmount"]
              ]
            ],
            [
              "收款情况",
              "",
              "",
              "",
              [
                ["开发票情况", "manage_drawMoney_invoiceAmount"],
                ["收款情况", "manage_drawMoney_recieve"],
                ["收款率", "collectionRate"]
              ]
            ],
            [
              "成本情况",
              "",
              "",
              "",
              [
                ["材料成本", "materialCost"],
                ["人工成本", "laborCost"],
                ["经营成本", "operatingCost"],
                ["合计成本", "totalCost"],
                ["利润", "profit"],
                ["利润率", "profitMargin"]
              ]
            ]
          ]
        ],
        ["备注", "remark"]
      ],
      cause_header: [
        ["事业部名称", "constuct_project_dep_name"],
        ["事业部负责人", "constuct_project_dep_leader"],
        [
          "经营汇总表",
          "",
          "",
          "",
          [
            [
              "合同情况",
              "",
              "",
              "",
              [
                ["合同金额", "manage_contract_amount"],
                ["最终结算金额", "settlementAmount"]
              ]
            ],
            [
              "收款情况",
              "",
              "",
              "",
              [
                ["开发票情况", "manage_drawMoney_invoiceAmount"],
                ["收款情况", "manage_drawMoney_recieve"],
                ["收款率", "collectionRate"]
              ]
            ],
            [
              "成本情况",
              "",
              "",
              "",
              [
                ["材料成本", "materialCost"],
                ["人工成本", "laborCost"],
                ["经营成本", "operatingCost"],
                ["合计成本", "totalCost"],
                ["利润", "profit"],
                ["利润率", "profitMargin"]
              ]
            ]
          ]
        ],
        ["备注", "remark"]
      ],
      activeitem: {},
      companyId: 0,
      isload: true
    };
  },
  components: {
    CaRuleTable,
    paging,
    selectCompany
  },
  created() {
    this.header = this.cause_header;
    this.getsummary();
  },
  methods: {
    save() {
      this.$confirm(`确定保存吗?`)
        .then(() => {
          switch (this.companyId) {
            case 16:
              this.addform.contract_id = this.activeitem.constuct_project_dep_id;
              apisaveProjectSummary(this.addform).then(res => {
                this.$message.success(res.msg);
                this.iscellCilck = false;
                // this.getsummary();
                this.getBuildSummary();
              });
              break;
            default:
              this.addform.projectDepId = this.activeitem.constuct_project_dep_id;
              apisaveProjectDepSummary(this.addform).then(res => {
                this.$message.success(res.msg);
                this.iscellCilck = false;
                this.getsummary();
              });
              break;
          }
        })
        .catch(() => {});
    },
    cellCilck(e) {
      console.log(e);
      this.activeitem = e;
      this.iscellCilck = true;
    },

    gelectCompany(e) {
      console.log(e);
      this.companyId = e.company_id;
    },
    querySumm() {
      this.currentlimit = 15;
      this.currentpage = 1;
      switch (this.companyId) {
        case 16:
          this.header = this.build_header;
          // "调用诚安建设接口"
          this.getBuildSummary();
          break;
        default:
          this.header = this.cause_header;
          this.getsummary();
          break;
      }
      this.isload = false;
      this.$nextTick(() => {
        this.isload = true;
      });
      // this.getsummary();
    },
    getpage(e) {
      this.currentpage = e;
      this.getsummary();
    },
    getlimit(e) {
      this.currentlimit = e;
      this.getsummary();
    },
    getBuildSummary() {
      let data = {
        year: this.yearsumm, //(选填)年度
        projectDepId: "", //(必填)项目部id
        pageSize: this.currentlimit, //(必填):每页显示多少数量
        limit: this.currentpage //(必填)当前页
      };
      apigetProjectSummaryByDep(data).then(res => {
        this.summaryList = res.data;
        this.total = res.total;
      });
    },
    getsummary() {
      let data = {
        year: this.yearsumm, //(选填) 年度
        pageSize: this.currentpage, //(必填):每页显示多少数量
        limit: this.currentlimit //(必填)当前页
      };
      apigetProjectDepSummary(data)
        .then(res => {
          this.summaryList = res.data;
          this.total = res.total;
        })
        .then(err => {});
    }
  }
};
</script>

<style lang="scss" scoped></style>
